<?php
class IndexAction extends Action
{
	
	//生成订单跳转支付页面
	public function tiangong(){
         $totalprice=0.1;
		 $note='购买物品';
		 $orderid='订单ID自己生成的';
        $url = 'http://' . $_SERVER ['SERVER_NAME']. U('Ad/Pay/pay',array('totalprice'=>$totalprice,'cart_names'=>$note,'orderid'=>$orderid));
        header("Location: $url");
        exit();
    }
	
	//跳转到支付页面等待按下支付
    public function pay(){
        $totalprice = $_GET['totalprice'];
        $cart_names = $_GET['cart_name'];
        $orderid = $_GET['orderid'];

        define('TEE_SITE_URL', 'http://teegon.com/');
        define('TEE_API_URL', 'http://api.teegon.com/');
        define('TEE_CLIENT_ID', 'in2dusddtxuo2a6kqafpagp7');  //ID
        define('TEE_CLIENT_SECRET', '4dtxr2nuris3ogf6yrmlkwc6ge23vh4s'); //密钥
        $param['order_no'] = $orderid; //订单号
        $param['channel'] = 'wxpay_jsapi';
        $param['return_url'] = 'http://' . $_SERVER ['SERVER_NAME'] . U("Ad/Pay/payover", $_GET);//回调
        $param['amount'] = $totalprice;
        $param['subject'] = "支付";
        $param['metadata'] = '';
        $param['notify_url'] = 'http://' . $_SERVER ['SERVER_NAME'] . U("Ad/Pay/paytg");//支付成功后异步回调通知
        $param['client_ip'] = get_client_ip();
        $param['client_id'] = 'in2dusddtxuo2a6kqafpagp7';  //ID

        import('Teegon', APP_PATH . 'Common/Wechat', '.class.php');
        $srv = new TeegonService('http://api.teegon.com/');
        $sign = $srv->sign($param);
        $param['sign'] = $sign;

//        $info = R("Api/Api/gettheme");
//        C("DEFAULT_THEME", $info ["theme"]);

        $this->assign("price", $order_info['totalprice']);
        $this->assign("info", $param);
        $this->assign("url", $param['return_url']);
        $this->assign("returnUrl", $param['return_url']);
        $this->display();
    }
	//2次跳转支付页面
	public function pay2(){
		
        header("Content-type: text/html; charset=UTF-8");
        import('Teegon', APP_PATH . 'Common/Wechat', '.class.php');

        if(!empty($_POST))
        {
            $path = 'v1/charge';
            $srv = new TeegonService('http://api.teegon.com/');
            $return = $srv->post($path,$_POST);
            $return=json_decode($return,1);
            $js = $return['result']['action']['params'];
            echo $html = "<html>
				<head>
				    <title>天工支付</title>
				</head>
				<body>
				<script>
				function Call()
				{
				$js;
				};
				Call();	
				</script>
				</body>
				</html>
				";
            exit;

        }
    }
	//回调
	 public function payover(){
        $agent = $_SERVER['HTTP_USER_AGENT'];
        /*if(empty($_SESSION['ad_uid']))
        {
            exit('请先关注该公众号');
        }*/
        if (empty($_SESSION['ad_uid'])) {
            $uid = $_GET['uid'];
        } else {
            $uid = $_SESSION['ad_uid'];
        }
        if (!$uid) {
            exit('请先关注该公众号');
        }


        $out_trade_no = $_GET['order_no'];
        $order_info = M("Order")->where(array('orderid' => $out_trade_no))->find();
        //if($order_info[pay_yz] == 1){exit('您已经付款过，请手工返回');} //防止刷佣金

        if (empty($order_info)) {
            exit('未找到订单信息');
        }
        //add 20150916
        /*if ($order_info['order_status'] != 0) {
            if (strpos($agent, "icroMessenger")) {
                $this->redirect('App/Index/member', array('uid' => $uid, 'page_type' => 'order'));

            } else {
                redirect('http://' . $_SERVER ['SERVER_NAME'] . '/paysuccess/success.html');
            }
        }*/

        $userdata = M("User")->where(array(
            "uid" => $uid, "id" => $uid, '_logic' => 'or'))->find();

        if (!$userdata) {
            exit('未找到用户信息');
        }

        if(strpos($agent,"icroMessenger")) {
            if ($order_info['id_ad']==1){
                redirect('http://' . $_SERVER ['SERVER_NAME'].'/paysuccess/successad.html');
            }else{
                $this->redirect('App/Index/member', array('uid'=>$uid,'page_type'=>'order'));
            }

        }else{
            M ("Order")->where(array('orderid'=>$out_trade_no))->save (array('pay_yz'=>1));			//防止刷佣金标记
            if ($order_info['id_ad']==1){
                redirect('http://' . $_SERVER ['SERVER_NAME'].'/paysuccess/successad.html');
            }else{
                redirect('http://' . $_SERVER ['SERVER_NAME'].'/paysuccess/success.html');
            }

        }

    }
	//异步回调处理
	public function paytg(){
	//回调信息{"amount":"0.10","bank":"CCB_DEBIT","buyer":"oNOeis9B34CBbE15wpwVtqJkd6gA","buyer_openid":"","channel":"wxpaymp_pinganpay","charge_id":"sf3rmclqayg9u7yrvmx4lw2t","device_info":"","is_success":"true","metadata":"","order_no":"20170113155019673291","pay_time":"1484293829","payment_no":"2017011315502101333142686540","real_amount":"0.10","sign":"BBE2C32799CFD84F50F9611DFCED6D1C","status":"","timestamp":"1484293830"}
        $out_trade_no = $_POST['order_no'];
		
		 echo "SUCCESS";
    }
}